<template>
  <div>
    <!--  node-key="id" 用来标记勾选状态 -->
    <el-tree
      ref="tree"
      node-key="id"
      show-checkbox
      default-expand-all
      check-strictly
      :data="list"
      :props="{ label: 'name' }"
    />
    <el-row slot="footer" type="flex" justify="end">
      <el-col :span="6">
        <el-button size="mini">取消</el-button>
        <el-button size="mini" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { tranToTerr } from '@/utils/indexp'
import { getRoleDetail, putAssignPrem } from '@/api/settings'

export default {
  name: 'VueHrAssignPermission',

  props: {
    curId: {
      type: String,
      required: true
    }
  },

  data() {
    return {
      list: []
    }
  },

  created() {
    this.lodePermissionList()
  },

  methods: {

    async hSubmit() {
      try {
        // 获取多选选中的树形节点
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await putAssignPrem({ id: this.curId, permIds: permIds })
        if (res.code !== 10000) return this.$message.error(res.message)
        // this.$message.success(res.message)
        this.$emit('success')
        console.log('putAssignPrem', res)
        // 保存数据
      } catch (err) {
        console.log('putAssignPrem', err)
      }
    },

    // 获取权限列表
    async lodePermissionList() {
      try {
        const res = await getPermissionList()
        // 获取角色详情
        const detail = await getRoleDetail(this.curId)
        // detail.data.permIds // 该角色拥有的权限
        this.$refs.tree.setCheckedKeys(detail.data.permIds) // 将角色拥有的权限，让对应的树形节点成勾选状态
        // console.log('getPermissionList', res)
        // 保存数据
        this.list = tranToTerr(res.data)
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
